#include "RepoManage.h"
#include <FlatpakRemotes.h>

RepoManage::RepoManage(QObject *parent) : QObject(parent) {
    

//  Manage remote repositories
//   remotes                List all configured remotes
//   remote-add             Add a new remote repository (by URL)
//   remote-modify          Modify properties of a configured remote
//   remote-delete          Delete a configured remote
//   remote-ls              List contents of a configured remote
//   remote-info            Show information about a remote app or runtime

    QString resutl = FlatpakCommand::RunCommand(QStringList()<< "remotes");

// flathub       system
// gnome-nightly system
// flathub       user

}

// flatpak remotes --show-details
QList<QMap<QString, QString>> RepoManage::repositories(const RepoType type) {
    // QStringList repositories;
    QList<QMap<QString, QString>> repositories;

    // 
    QString content = Flatpak::Remotes()
        .column_name()
        .column_title()
        .column_url()
        .column_collection()
        .column_priority()
        .column_options()
        .ostree_verbose().run();

    if (!content.isEmpty()) {
         QStringList splits = content.split("\n");
         foreach(const QString& item, splits) {
            QStringList repository = item.split("\t");
            QTextStream out(stdout);
            out << repository.join(",") <<  "\n";
            if (repository.length() == 6) {
                QMap<QString, QString> repo;
                repo["Name"] = repository[0];
                repo["Title"] = repository[1];
                repo["Url"] = repository[2];
                repo["Collection_ID"] = repository[3];
                repo["Priority"] = repository[4];
                repo["Options"] = repository[5];
                repositories.append(repo);
            }
         }
    }
    return repositories;

// > flatpak remotes --show-details
// Name          Title                            URL                             Collection ID Priority Options
// flathub       Flathub                          https://dl.flathub.org/repo/    -             1        system
// gnome-nightly org.gtk.Demo4 from gnome-nightly https://nightly.gnome.org/repo/ -             1        system
// flathub       Flathub                          https://dl.flathub.org/repo/    -             1        user

}




//         ID: org.gtk.Demo4
//        Ref: app/org.gtk.Demo4/x86_64/master
//       Arch: x86_64
//     Branch: master
// Collection: org.gnome.Nightly
//   Download: 50.3 MB
//  Installed: 209.0 MB
//    Runtime: org.gnome.Platform/x86_64/master

//        Sdk: org.gnome.Sdk/x86_64/master
//     Commit: 3d51d4aeb539c7fe2a0c6b38abfe0b74ed5c76a7812961106bb4e4ad025a5ee3
//     Parent: 4b4998563851b1b3fe09beac54b31d065ceb0db9086077c111dd0ce85dec682e
//    Subject: Export org.gtk.Demo4
//       Date: 2022-01-03 13:57:19 +0000


